package com.zx.ext.controller;


import com.zx.ext.entity.Books;
import com.zx.ext.service.BookServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书管理Controller层
 */
@Controller
@Transactional
@ResponseBody
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookServices bookServices;

    //添加或删除方法图书方法
    @RequestMapping(value = "/addBook", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addBook(HttpServletRequest request) {

        Map<String, Object> modelmap = new HashMap<>();

        try {
            //将前台传过来的数据封装到对象中
            Books books = new Books();
            String bookName = request.getParameter("bookName");
            String bookCounts = request.getParameter("bookCounts");
            String details = request.getParameter("details");

            //将前台的值赋值给books对象
            books.setBookName(bookName);
            books.setBookCounts(Integer.parseInt(bookCounts));
            books.setDetails(details);

            int effects = bookServices.addBook(books);//存入数据库返回影响行数
            if (effects >= 0) {
                modelmap.put("success", true);
                modelmap.put("Msg", "添加成功");
            } else {
                modelmap.put("success", false);
                modelmap.put("Msg", "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return modelmap;
    }

    //删除图书方法
    @RequestMapping("/delBook")
    @ResponseBody
    public Map<String, Object> delBook(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();

        //接收前端前端传来的bookId
        String bookId = request.getParameter("bookId");
        System.out.println(bookId);
        try {
            int effectNum = bookServices.delBook(Integer.parseInt(bookId));
            if (effectNum > 0) {
                modelMap.put("success", true);
            } else {
                modelMap.put("success", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelMap;
    }

    //更新图书方法
    @ResponseBody
    @RequestMapping(value = "/updateBook", method = RequestMethod.POST)

    public Map<String, Object> updateBook(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();

        //将前台传过来的数据封装到对象中
        Books books = new Books();
        String bookId = request.getParameter("bookId");
        String bookName = request.getParameter("bookName");
        String bookCounts = request.getParameter("bookCounts");
        String details = request.getParameter("details");

        //将前台的只赋值给对象
        books.setBookId(Integer.parseInt(bookId));
        books.setBookName(bookName);
        books.setBookCounts(Integer.parseInt(bookCounts));
        books.setDetails(details);

        try {
            int effectNum = bookServices.updateBook(books);
            if (effectNum > 0) {
                modelMap.put("success", true);
            } else {
                modelMap.put("success", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelMap;
    }

    //查询所有图书方法
    @RequestMapping("/queryAllBooks")
    @ResponseBody
    public Map<String, Object> queryBook(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();

        try {
            List<Books> allBooks = bookServices.queryBooks();
            modelMap.put("success", true);
            modelMap.put("books", allBooks);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelMap;
    }

}
